﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

namespace DocumentGenerator.Document
{
    public class DocumentESEML : IDocument
    {
        iTextSharp.text.Document document;
        private int chapterIndex;

        public DocumentESEML(string path)
        {
            document = new iTextSharp.text.Document(PageSize.A4);
            Font f = new Font(iTextSharp.text.Font.FontFamily.COURIER, 20, Font.BOLD);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
            writer.InitialLeading = 16;
            chapterIndex = 0;
        }

        public void Open()
        {
            this.document.Open();
        }

        public void Close()
        {
            this.document.Close();
        }

        public void NewPage()
        {
            this.document.NewPage();
        }

        public void AddCover(string name, string description)
        {
            for(int i = 0; i<=20; i++)
                this.document.Add(Chunk.NEWLINE);


            Font f = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD);
            Paragraph p = new Paragraph(name, f);
            p.Alignment = Element.ALIGN_CENTER;
            this.document.Add(p);

            f = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.NORMAL);
            p = new Paragraph(description, f);
            p.Alignment = Element.ALIGN_CENTER;

            this.document.Add(p);
            

            for (int i = 0; i <= 9; i++)
                this.document.Add(Chunk.NEWLINE);

            string date = DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year;

            f = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
            p = new Paragraph(date, f);
            p.Alignment = Element.ALIGN_CENTER;

            this.document.Add(p);

            this.document.NewPage();
      
        }

        public void AddParagraph(string value, int size)
        {
            Paragraph p = new Paragraph(value, new Font(Font.FontFamily.TIMES_ROMAN, size, Font.NORMAL));
            p.Alignment = Element.ALIGN_JUSTIFIED;
            this.document.Add(p);
        }

        public void AddChunk(string value, int size, int fontStyle)
        {
            Chunk chunk = new Chunk(value, new Font(Font.FontFamily.TIMES_ROMAN, size, fontStyle));
            this.document.Add(chunk);
        }
        public Chapter AddChapter(string description)
        {
            Chapter chapter = new Chapter(new Paragraph(description, new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD)), ++chapterIndex);
            this.document.Add(chapter);
            return chapter;
        }

        public Section AddSection(Chapter chapter, string description)
        {
            Paragraph p = new Paragraph(description, new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD));
            Section section = chapter.AddSection(p);
            section.Indentation = 30;
            section.NumberStyle = Section.NUMBERSTYLE_DOTTED;
            this.document.Add(section);
            return section;
        }

        public Section AddSubSection(Section section, string description)
        {
            Paragraph p = new Paragraph(description, new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL));
            Section s = section.AddSection(p);
            s.Indentation = 30;
            s.NumberStyle = Section.NUMBERSTYLE_DOTTED;
            this.document.Add(s);
            return s;
        }

        public void AddList(iTextSharp.text.List list)
        {
            this.document.Add(list);
        }
    }   
}
